﻿using Desktop.Controls;
using Desktop.Interfaces;
using Desktop.Library;
using Google.GData.Client;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace StoryWriter
{
    /// <summary>
    /// Interaction logic for BlogMaintenance.xaml
    /// </summary>
    public partial class BlogMaintenance : ActivityBase
    {
        #region Constructors

        public BlogMaintenance(IWorkspace parent)
            : base(parent)
        {
            InitializeComponent();

            IsCloseable = false;
            
            LoadBlogList();
        }

        #endregion

        #region Properties

        private BlogManager BlogManager
        {
            get
            {
                return feedChooserComboBox.DataContext as BlogManager;
            }
            set
            {
                feedChooserComboBox.DataContext = value;
            }
        }

        private Blog Blog
        {
            get
            {
                return feedListView.DataContext as Blog;
            }
            set
            {
                feedListView.DataContext = value;
            }
        }

        #endregion

        #region Methods

        private void LoadBlogList()
        {
            try
            {
                if (!Thread.CurrentPrincipal.Identity.IsAuthenticated)
                {
                    BlogManager = new BlogManager();
                    Blog = null;
                }
                else
                {
                    BlogManager = new BlogManager(Thread.CurrentPrincipal.Identity as GoogleIdentity);
                }
                if (BlogManager.Count == 0)
                {
                    noFeedsLabel.Visibility = Visibility.Visible;
                    feedChooserComboBox.Visibility = Visibility.Hidden;
                    feedChooserComboBox.SelectedItem = null;
                }
                else
                {
                    noFeedsLabel.Visibility = Visibility.Hidden;
                    feedChooserComboBox.Visibility = Visibility.Visible;
                    feedChooserComboBox.SelectedItem = BlogManager[0];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Event Handlers

        private void ActivityBase_PrincipalChanged(object sender, RoutedEventArgs e)
        {
            LoadBlogList();
        }

        private void feedChooserComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (feedChooserComboBox.SelectedItem == null)
            {
                return;
            }
            AtomEntry entry = feedChooserComboBox.SelectedItem as AtomEntry;

            toolbar.IsEnabled = false;

            try
            {
                Blog = BlogManager.GetBlog(entry);
                if (Blog != null)
                {
                    toolbar.IsEnabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            if (Blog == null)
            {
                return;
            }

            Parent.OpenActivity(new EntryEditor(Blog, null));

            /*
            AtomEntry entry = new AtomEntry();
            entry.Content.Type = "html";

            if ((new EntryEditor()
            {
                DataContext = entry
            }).ShowDialog().GetValueOrDefault(false))
            {
                return;
            }

            Blog.InsertEntry(entry);
            */
        }

        private void OnEdit(object sender, RoutedEventArgs e)
        {
            AtomEntry entry = feedListView.SelectedItem as AtomEntry;
            if ((entry == null) || (Blog == null))
            {
                return;
            }

            Parent.OpenActivity(new EntryEditor(Blog, entry));
            
            /*
            if (!(new EntryEditor()
            {
                DataContext = entry
            }).ShowDialog().GetValueOrDefault(false))
            {
                return;
            }

            Blog.UpdateEntry(entry);
            */
        }

        private void OnRemove(object sender, RoutedEventArgs e)
        {
            if (Blog == null)
            {
                return;
            }
            Blog.DeleteEntry(feedListView.SelectedItem as AtomEntry);
        }

        private void feedListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            OnEdit(sender, null);
        }

        #endregion
    }
}